package com.jeesite.modules.jydq.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.mybatis.mapper.query.QueryType;
import com.jeesite.modules.sys.utils.RoleUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.jydq.entity.JydqUserFavorite;
import com.jeesite.modules.jydq.service.JydqUserFavoriteService;

/**
 * 用户收藏表Controller
 * @author dong
 * @version 2025-03-20
 */
@Controller
@RequestMapping(value = "${adminPath}/jydq/userFavorite")
public class JydqUserFavoriteController extends BaseController {

	@Autowired
	private JydqUserFavoriteService jydqUserFavoriteService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public JydqUserFavorite get(String id, boolean isNewRecord) {
		return jydqUserFavoriteService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
//	@RequiresPermissions("jydq:userFavorite:view")
	@RequestMapping(value = {"list", ""})
	public String list(JydqUserFavorite jydqUserFavorite, Model model) {
		model.addAttribute("jydqUserFavorite", jydqUserFavorite);
		return "modules/jydq/jydqUserFavoriteList";
	}
	
	/**
	 * 查询列表数据
	 */
//	@RequiresPermissions("jydq:userFavorite:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<JydqUserFavorite> listData(JydqUserFavorite jydqUserFavorite, HttpServletRequest request, HttpServletResponse response) {
		if(!RoleUtils.hasCurrentUserRole("corpAdmin") && !RoleUtils.hasCurrentUserRole("manage")){
			jydqUserFavorite.sqlMap().getWhere().and("create_by", QueryType.EQ, UserUtils.getUser().getUserCode());
		}
		jydqUserFavorite.setPage(new Page<>(request, response));
		Page<JydqUserFavorite> page = jydqUserFavoriteService.findPage(jydqUserFavorite);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
//	@RequiresPermissions("jydq:userFavorite:view")
	@RequestMapping(value = "form")
	public String form(JydqUserFavorite jydqUserFavorite, Model model) {
		model.addAttribute("jydqUserFavorite", jydqUserFavorite);
		return "modules/jydq/jydqUserFavoriteForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("jydq:userFavorite:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated JydqUserFavorite jydqUserFavorite) {
		JydqUserFavorite verify = new JydqUserFavorite();
		verify.setProductId(jydqUserFavorite.getProductId());
		verify.sqlMap().getWhere().and("create_by", QueryType.EQ, UserUtils.getUser().getUserCode());
		long count = jydqUserFavoriteService.findCount(verify);
		if (jydqUserFavoriteService.findCount(verify) > 0){
			return renderResult(Global.FALSE, text("请勿重复收藏！"));
		}
		jydqUserFavoriteService.save(jydqUserFavorite);
		return renderResult(Global.TRUE, text("保存用户收藏表成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("jydq:userFavorite:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(JydqUserFavorite jydqUserFavorite) {
		jydqUserFavoriteService.delete(jydqUserFavorite);
		return renderResult(Global.TRUE, text("删除用户收藏表成功！"));
	}
	
}